package com.leaves.crontab;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leaves.common.publicmethod.methods.MapperUtil;
import com.leaves.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2023年01月18日  10时33分41秒
 * @Description: 定时任务
 */
@Slf4j
@Configuration
@EnableScheduling   // 1.开启定时任务
public class TimedTask {

    @Resource
    private MapperUtil mapperUtil;

//    @Async
//    @Scheduled(cron = "*/10 * * * * ?")
    public void autoUntieDisableUserInfo() {
        log.info("------------定时任务1_____自动解除封禁_____开始执行------------");
        List<UserInfo> userInfoList = mapperUtil.userInfoMapper.selectList(new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getEnable, 2));
        userInfoList.forEach(userInfo -> {
            System.out.println("userInfo.getId() = " + userInfo.getId());
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime ldt = userInfo.getDisableTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            System.out.println("now.isAfter(ldt) = " + now.isAfter(ldt));
            System.out.println("now.isEqual(ldt) = " + now.isEqual(ldt));
            System.out.println("now.isAfter(ldt) || now.isEqual(ldt) = " + (now.isAfter(ldt) || now.isEqual(ldt)));
//            boolean judge = DateUtils.judgeTime(new Date(), userInfo.getDisableTime());
//            System.out.println("judge = " + judge);
            boolean isDisable = StpUtil.isDisable(userInfo.getId());
            System.out.println("isDisable = " + isDisable);
            if (now.isAfter(ldt) || now.isEqual(ldt)) {
//            if (judge) {
                if (!isDisable) {
                    userInfo.setEnable(1);
                    userInfo.setDisableTime(null);
                    int i = mapperUtil.userInfoMapper.updateEnableById(userInfo);
                    System.out.println("i = " + i);
                }
            }

        });
        log.info("------------定时任务1_____自动解除封禁_____执行结束------------");
    }
}
